package geppetto.phraseTable.phrase.feature.calc.local;

import geppetto.phraseHMM.phraseExtraction.extractedphrase.ExtractedPhrasePairData;
import geppetto.phraseHMM.phraseExtraction.extractedphrase.data.ScoreData;
import geppetto.phraseTable.phrase.Phrase;
import geppetto.phraseTable.phrase.feature.AbstractFeature;
import geppetto.phraseTable.phrase.feature.ProbabilityFeature;

import java.util.ArrayList;
import java.util.HashMap;


public class ProbabilityFeatureCalc implements AbstractFeatureCalc{
	
	protected final static String SCORE_HEADER = "score";
	protected final static String FEATURE = "prob";
	protected int order;
	
	public ProbabilityFeatureCalc(int order) {
		super();
		this.order = order;
	}

	public AbstractFeature[] calculateFeature(ArrayList<String> source, ArrayList<String> target, ArrayList<HashMap<String, ExtractedPhrasePairData>> data) {
		AbstractFeature[] featureArray = new ProbabilityFeature[data.size()];
		double totalScore = 0;
		for(int i = 0; i < featureArray.length; i++){			
			double score = ((ScoreData)data.get(i).get(SCORE_HEADER)).getScore();
			totalScore+=score;
		}
		for(int i = 0; i < featureArray.length; i++){
			double score = ((ScoreData)data.get(i).get(SCORE_HEADER)).getScore();
			featureArray[i] = new ProbabilityFeature(score/totalScore);
		}
		return featureArray;
	}

	@Override
	public void calculateFeature(ArrayList<Phrase> phrases) {
		double totalScore = 0;
		for(int i = 0; i < phrases.size(); i++){			
			Phrase p = phrases.get(i);
			double score = ((ScoreData)p.getData(SCORE_HEADER)).getScore();
			totalScore+=score;
		}
		for(int i = 0; i < phrases.size(); i++){
			Phrase p = phrases.get(i);
			double score = ((ScoreData)p.getData(SCORE_HEADER)).getScore();
			phrases.get(i)._features.addFeature(FEATURE,new ProbabilityFeature(score/totalScore), order);
		}
	}

}
